/*
 * RefCount.cpp
 *
 *  Created on: Nov 10, 2008
 *      Author: brain
 */

#include "RefCount.h"

int RefCount::diff_instances=0;
RefCount::RefCount(IplImage * im)
{
	count=0;
	if (im==0)
		throw -1;
	_data=im;
	imdata=reinterpret_cast<unsigned char *>(im->imageData);
	RefCount::diff_instances++;
	color_bits=(((1<<(depth()-1))-1)<<1)+1;

	if (DEBUG)
        cout<< "data created, existing: "<<RefCount::diff_instances<<endl;
	count++;
}




RefCount::~RefCount()
{

	cvReleaseImage( &_data );
	--RefCount::diff_instances;


	if (DEBUG)
	{
		setforeb(green);
		cout<< "data released,left: "<<RefCount::diff_instances <<endl;
        setfore(white);
	}

}
void RefCount::increase()
{
	++count;
}
void RefCount::decrease()
{
	--count;
	if (count==0)
	{
		delete this;
	}
}
void RefCount::printcount()
{
    cout<< count <<endl;
}
unsigned char* RefCount::ImData()
{
	return imdata;
}

uint RefCount::depth()
{

	return _data->depth&(~(1<<31));

}
